package hasieraketa;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class Hasieraketa {

	/**
	 * @param args
	 */
	static String []izenPos;//KENDU!!!!!!!!!!!!!!!!
	static String leader=null;
	static Hashtable <String,Integer> candidates=new  Hashtable<String,Integer> ();
	static int [] recovered=new int[50];//zenbaki haundi bat jarri da arraya static moduan jarri ahal izateko honela klasea kargatzean nahiko memoria hartu du gure arrayarentzat. 80. hilaran gure arrayaren benetako tamaina hartuko du, betiere ongi funtzionatuko du jarri diogun baina zenbaki txikiagoa bada benetako tamaina.  
	static int timer;
	static int zenbakiKonstantea=5000;
	static int incarnation;
	
	static void update_leader(){
		int minimoa;
		minimoa=recovered[0];
		String leaderIzena=null;
		String izena;
		MakinaBektorea makinak= MakinaBektorea.getInstance();
		/*****************************PROBAK***********************************************
		 */
		for (Enumeration <String>e =candidates.keys();e.hasMoreElements();){System.out.println(e.nextElement());}
			/*****************************AMAITU PROBAK***********************************************
			 */
		
		for (Enumeration <String>e =candidates.keys();e.hasMoreElements();){
			izena=e.nextElement();
			Makina unekoMakina=makinak.hash.get(izena);
			System.out.println("update leaderreko makinaren izena: "+izena);
			if (unekoMakina==null)
				System.out.println(izena+"makina ez da existitzen");
				System.out.println(unekoMakina.pos);

			if (minimoa>recovered[unekoMakina.pos]){
				minimoa=recovered[unekoMakina.pos];
				leaderIzena=izena;
			}
		}
//		while(candidates.size()>pos){
//			if (minimoa>recovered[pos]){
//			minimoa=recovered[pos];
//			posizioa=pos;
//		}
//				pos++;
//		}
		leader=leaderIzena;
		timer=makinak.hash.get(leader).timeout;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			MakinaBektorea makinak= MakinaBektorea.getInstance();

			//singelton moduan jarri
			//singelton moduan jarri

			FileReader irakurriIncarnation = new FileReader ("incarnation");
			BufferedReader irakurri = new BufferedReader (irakurriIncarnation);
			String zenbakia=irakurri.readLine();
			irakurri.close();
			irakurriIncarnation.close();
			//incarnation irakurri da
			FileWriter idatzi= new FileWriter("incarnation");
			incarnation = Integer.parseInt(zenbakia);
			idatzi.write(Integer.toString(++incarnation));
			idatzi.close();
			//incarnation berria idatzi da;
			FileReader irakurriLeader = new FileReader ("leader");
			BufferedReader leaderIrakurri = new BufferedReader (irakurriLeader);
			leader=leaderIrakurri.readLine();
			//leader=new StringBuilder(leaderra);
			System.out.println("leaderra:"+leader);
			leaderIrakurri.close();
			irakurriLeader.close();
			//Hashtable <String, String> candidates= new Hashtable<String, String>();
			String makinaIzena=InetAddress.getLocalHost().getHostName();
			//if (!makinak.hash.get(makinaIzena).candidate)
				candidates.put(makinaIzena,0);
				candidates.put(leader,0);
				System.out.println("hau gehituda candidatesera makina izena bezala: "+makinaIzena);
				System.out.println("hau gehituda candidatesera leader bezala: "+leader);
				recovered=new int[makinak.hash.size()];//makina barruen jarri ezkeo, gero mezu baten bialtzeakuen hash osue bialduber
				izenPos=new String [makinak.hash.size()];//optimuo beheko for-a aprobetxauta array bat sortzie ta hau bialtzie.
				int recoveredPos=0;
				
		
			for (Enumeration <String> e=makinak.hash.keys(); e.hasMoreElements();){
				String makina=e.nextElement();
				izenPos[recoveredPos]=makina;
				if(makinaIzena.compareTo(makina)==0)
					recovered[makinak.hash.get(makina).pos]=incarnation;
				else{
					makinak.hash.get(makina).timeout= zenbakiKonstantea+incarnation;
					recovered[makinak.hash.get(makina).pos]=0;
					}
				recoveredPos++;
			}

			if(makinaIzena.compareTo(leader.toString())!=0)
				timer=makinak.hash.get(Hasieraketa.leader).timeout;
			
			//PROBAK**************************************************
			int i=0;
			for (Enumeration <Makina> e  = makinak.hash.elements();e.hasMoreElements();){
				Makina mak=e.nextElement();
				System.out.println(mak.pos+"\n");
				System.out.println(recovered[i++]);
				System.out.println(mak.timeout+"\n");
				System.out.println("\\\\\\\\\\\\\\\\\\\\\n");
			}
				System.out.println(timer);
			//PROBA AMAIERA******************************************	
		System.out.println("hasieraketa atala burutu da");
			//PROZESUEN HASIERAKETA
			Task1 t1= new Task1();
			t1.start();
			Task2 t2= new Task2();
			t2.start();
			Task3 t3= new Task3();
			t3.start();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
